<template lang="jade">
	div(class="panel panel-default",v-loading="loading")
		div(class="panel-heading")
			h3(class="panel-title") 产品类型基本信息

		div(class="panel-body")
			el-form(ref="form",label-width="100px",size="small")
				el-form-item(label="类别名称")
					el-input(v-model="productType.name",placeholder="请输入类别名称")
				el-form-item(label="备注")
					el-input(v-model="productType.remark", type="textarea")
					
		div(class="panel-footer clearfix")
			div(class="pull-right")
				button(class="btn btn-primary",@click="save") 保存
</template>
<script>
	import _ from 'lodash';
	import ProductType from '../models/product-type';
	import {request} from 'collection';

	export default{
		data(){
			return {loading:false};
		},
		props:{
			productType: {type: ProductType, default: new ProductType}
		},

		model:{
			prop:'productType',
			event:'update'
		},

		methods:{
			save() {
				if(this.productType.properties.length == 0){
					this.$message({message:'至少需要包含1个属性',type:'warning'});
					return;
				}
				var index =_.findIndex(this.productType.properties, function(property){
					return property.items.length === 0;
				});
				if(index>-1){
					var message = '请添加"'+this.productType.properties[index].name+'"的属性值';
					this.$message({message:message,type:'warning'});
					return;
				}
				
				var method = this.productType.id ? request.put : request.post;
				var url = this.productType.id ? '/api/services/app/productType/update' : '/api/services/app/productType/create'
				var params = _.extend({}, this.productType);

				var vm = this;
				method.call(this, url, params).then((res)=> {
					vm.$message({type:'success',message:'保存成功'});
					this.$router.push('/product-type-list')
				})
			}
		}
	}
</script>

